package com.example.musicapp.view;



import android.content.Intent;

import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.musicapp.R;
import com.example.musicapp.base.BaseActivity;
import com.example.musicapp.bean.User;
import com.example.musicapp.contract.ILoginContract;
import com.example.musicapp.presenter.LoginPresenter;


import static com.example.musicapp.utils.Constant.REQUEST_REGISTER;

public class LoginActivity extends BaseActivity<ILoginContract.LoginPre> implements ILoginContract.ILoginUi, View.OnClickListener {


    //一个一个地初始化数据
    private EditText mEdPassword;
    private EditText mEtAccount;
    private String mPassword;
    private String mAccount;
    private Button mBtnLogin;
    private TextView mBtnRegister;
    private ImageView mIvRegister;
    private User mUser = new User();



    @Override
    public ILoginContract.LoginPre onBindPresenter() {
        return new LoginPresenter(this) ;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mEtAccount = (EditText) findViewById(R.id.et_username);
        mEdPassword = (EditText) findViewById(R.id.et_password);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mBtnRegister = findViewById(R.id.btn_register);
        mIvRegister = findViewById(R.id.iv_register);
        mBtnRegister.setOnClickListener(this);
        mIvRegister.setOnClickListener(this);
        mBtnLogin.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    public void loginSuccess(String username) {
        Toast.makeText(this,"登录成功!",Toast.LENGTH_LONG).show();
        Intent intent = new Intent();
        intent.putExtra("username",username);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    public void loginFailure() {
        Looper.prepare();
        Toast.makeText(this,"很遗憾，登录失败!",Toast.LENGTH_LONG).show();
        setResult(RESULT_CANCELED);
        finish();
        Looper.loop();
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                mAccount = mEtAccount.getText().toString();
                mPassword = mEdPassword.getText().toString();
                login(mAccount,mPassword);
                break;
            case R.id.iv_register :
            case R.id.btn_register:
                turnToRegister();
                break;
            default:
                break;
        }
    }


    private void turnToRegister() {
        Intent turnRegisterIntent = new Intent(LoginActivity.this,RegisterActivity.class);

        startActivityForResult(turnRegisterIntent,REQUEST_REGISTER);
    }


    private int login(String account, String password) {
//        if(mUser!= null){
//            mUser.setUsername(account);
//            mUser.setPassword(password);
//        }
//
//        int scanJudge = getPresenter().login(account,password);
//        switch (scanJudge){
//            case SCAN_EMPTY:
//                Toast.makeText(this,"请不要留空",Toast.LENGTH_LONG).show();
//                break;
//            case ACCOUNT_CONTAIN_BLANK:
//                Toast.makeText(this,"用户名不要输入空格",Toast.LENGTH_LONG).show();
//                break;
//            case LOGIN_ERROR_SCAN:
//                Toast.makeText(this,"用户名或密码输入错误",Toast.LENGTH_LONG).show();
//                break;
//            default:
//                break;

    getPresenter().login(mEtAccount.getText().toString(),mEdPassword.getText().toString());


        return 0;
    }

}
